<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;

class CommonEmailApi extends Controller
{	
	public function json($code, $msg, $data = [])
    {
        return response()->json([
        'status' => $code,
        'msg' => $msg,
        'data' => $data,
        ]);
    }

    public function sendEmailMsg(Request $Request)
    {
        
    	$toemail = $Request->input('email');
        $name = '亲爱的用户';
        $code = mt_rand(1000,9999);
        //dd($toemail);
        //将验证码存到session
        $Request->session()->put('emailcode', $code);

        $data = ['email'=>$toemail, 'name'=>$name, 'activationcode'=>$code];

         // 第一个参数填写模板的路径，第二个参数填写传到模板的变量
         Mail::send('/Home/Email/FotgotPassword', $data, function ($message) use($toemail){

            // 发件人（你自己的邮箱和名称）
            $message->from('164722305@qq.com', 'biggun');
            // 收件人的邮箱地址
            $message->to($toemail);
            // 邮件主题
            $message->subject('邮箱验证码');
        });
    }

    //验证码验证
    public static function checkEmailCode (Request $request, $formCodeName = 'emailcode')
    {
       $userCode = $request->input($formCodeName);
       if ( $request->session()->get('emailcode') == $userCode ) {
            return  true;
       }
            return false;
    }
}
